<?php
add_action('admin_footer', 'email_process');
function email_process()
{
?>
    <script type="text/javascript">
    jQuery(document).ready(function($)
    {
        // Show/hide listed email send
        $('#toggle').click(function(){
            $('#list_sended').toggle();
        });
        // show Reload link when date change
        $('#date').keyup(function(){
            $('#reload').css('visibility','');
            $('#reload').attr('href','<?php echo admin_url("/admin.php?page=email-action&date="); ?>' + $('#date').val());
        })

        // hide link send email before process Prepare send email
        //$('#run_send_email').hide();
        // Prepare : insert to wp_emails
        $('#prepare_send').click(function(){
            $('#status_mess').text("Processing ...");
            var vl = new Array();
            vl[0] = $('#date').val();

            var data = {action:'prepare_send',value:vl};
            $.post(ajaxurl, data, function(rs) {
                $('#status_mess').text(rs).show();
                alert(rs);
                location.reload();
                // If ready, show link send email
//                if(rs == 'Success! Ready to send')
//                    $('#run_send_email').show();
            });
        })
        // Send email
        $('#run_send_email').click(function(){
            send_emails();
        })
        function send_emails()
        {
            $('#status_mess').text("Sending ...");
            var data = {action:'send_email_new_cp'};
            $.post(ajaxurl, data, function(rs) {
                foo(rs);
            });
        }
        // check if rs > 0, loop send_emails()
        function foo(rs)
        {
            if(rs > 0)
                send_emails();
            else
            {
                update_last_send();
                $('#status_mess').text('Completed!').show();
                alert('Sended to Subscribers !');
                location.reload();
            }

        }
        //update time last send
        function update_last_send()
        {
            var data = {action:'update_last_send'};
            $.post(ajaxurl, data, function(rs) {

            });
        }
    });
    </script>
<?php
}
add_action('wp_ajax_update_last_send', 'update_last_send_cb');
function update_last_send_cb()
{
    update_option('last_send_email_newcp',date("Y-m-d G:i:s",time() + 25200));
    die();
}

add_action('wp_ajax_prepare_send', 'prepare_send_cb');
function prepare_send_cb()
{
    $value = $_POST['value'];
    $date = $value[0];

    if(get_option('emto_subscriber') == 1)
    {
        cpx_doaction_sendemail_daybyday($date);
        echo "Success! Ready to send";
        die();
    }
    else
    {
        echo 'This function disabled';
        die();
    }
}
add_action('wp_ajax_send_email_new_cp', 'send_email_new_cp_cb');
function send_email_new_cp_cb()
{
    global $wpdb;
    $send_time = date("Y-m-d G:i:s",time() + 25200);
    $arr = em_get_email_tosend();
    if(count($arr) > 0)
    {
        foreach ($arr as $a) {
            $arr_cp_ids = explode(',', $a->cp_ids);
            cpx_email_daybyday($a->em_to, $arr_cp_ids, $a->st_id,$a->follow_id);
            $qr = "UPDATE wp_emails SET status = 1,send_time = '{$send_time}' where ID = {$a->ID}";
            $wpdb->query($qr);
            //sleep(5);
        }
    }
    echo count($arr);
    die();
}
?>
